﻿/*
 * Project	    : Outlook 2003 Style Navigation Pane
 *
 * Author       : Muhammed ŞAHİN
 * eMail        : muhammed.sahin@gmail.com
 *
 * Description  : Button for OverFlowPanel
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace YT.Controls.OutlookStyleNavigateBar
{
    /// <summary>
    /// OverFlowPanel button control
    /// </summary>
    [ToolboxItem(false)]
    class NavigateBarOverFlowPanelButton : UserControl
    {
        // change for arrow button draw area
        const int ARROW_WIDTH = 6;
        const int ARROW_HEIGHT = 3;

        #region IsSelected
        bool isSelected = false;
        [Category("NavigateBarButton")]
        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                isSelected = value;
                Invalidate();
            }
        }
        #endregion

        #region IsOnOverFlowPanel
        bool isOnOverFlowPanel = false;
        public bool IsOnOverFlowPanel
        {
            get { return isOnOverFlowPanel; }
            set { isOnOverFlowPanel = value; }
        }
        #endregion

        #region IsArrowButton
        bool isArrowButton = false;
        public bool IsArrowButton
        {
            get { return isArrowButton; }
            set
            {
                if (value)
                    this.Cursor = Cursors.Default;
                isArrowButton = value;
            }
        }
        #endregion

        #region NavigateBarButton
        NavigateBarButton navigateBarButton;
        public NavigateBarButton NavigateBarButton
        {
            get { return navigateBarButton; }
            set
            {
                if (navigateBarButton != null)
                {
                    navigateBarButton = value;
                    if (navigateBarButton.NavigateBar != null)
                        Invalidate();

                }

            }
        }
        #endregion

        static ToolTip toolTip = new ToolTip();

        #region Yapıcı Methodlar

        public NavigateBarOverFlowPanelButton(NavigateBarButton tNavigateBarButton)
        {
            navigateBarButton = tNavigateBarButton;
            InitOverFlowButton();
        }

        void InitOverFlowButton()
        {
            this.Cursor = Cursors.Hand;
            this.Width = NavigateBar.OVER_FLOW_BUTTON_WIDTH;
            this.Height = NavigateBar.BUTTON_HEIGHT - 2; // Alt üst çizgilerin görünmesi için -2 ve 1 // for display button line (top & bottom)
            this.Top = 1;
            toolTip.ShowAlways = true;
            toolTip.InitialDelay = 200;
            toolTip.AutomaticDelay = 200;
        }
        #endregion

        #region Overrided Methodlar
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            PaintThisControl(IsSelected ? PaintType.Selected : PaintType.Normal);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            PaintThisControl(IsSelected ? PaintType.Selected : PaintType.MouseOver);
            toolTip.SetToolTip(this, navigateBarButton.ToolTipText);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            PaintThisControl(IsSelected ? PaintType.Selected : PaintType.Normal);
            toolTip.RemoveAll();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (!this.IsSelected)
                PaintThisControl(PaintType.Pressed);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            PaintThisControl(this.IsSelected ? PaintType.Selected : PaintType.Normal);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            toolTip.RemoveAll();
            // Only left click
            if (e.Button == MouseButtons.Left) // Sadece sol click ile button seçilebilmeli
            {
                IsSelected = true;
                PaintThisControl(PaintType.Selected);
            }
        }
        #endregion

        #region Diğer Metodlar
        void PaintThisControl(PaintType tPaintType)
        {

            Image imageButton = null;
            switch (tPaintType)
            {
                case PaintType.Normal:
                    {
                        imageButton = isSelected ? navigateBarButton.SelectedImage : navigateBarButton.Image;
                        break;
                    }
                case PaintType.Selected:
                    {
                        imageButton = navigateBarButton.SelectedImage;
                        break;
                    }
                case PaintType.MouseOver:
                    {
                        imageButton = navigateBarButton.MouseOverImage;
                        break;
                    }
                case PaintType.Pressed:
                    {
                        imageButton = navigateBarButton.Image;
                        break;
                    }
            }
            // Gradient olarak boyama işlemi
            // Paint gradient

            NavigateBarHelper.PaintGradientBack(this,
                navigateBarButton.NavigateBar.NavigateBarColorTable,
                tPaintType);

            // Image

            // OverFlowPanelde mutlaka bir image gösterilmeli
            // Image must be on panel
            if (imageButton == null)
                imageButton = Properties.Resources.NoImage;

            if (!this.Enabled)
                imageButton = navigateBarButton.DisableImage;


            if (!this.IsArrowButton) // Draw image 
            {

                Size imgSize = new Size(16, 16);
                int left = (this.Width - imgSize.Width) / 2;
                int top = (this.Height - imgSize.Height) / 2 + 1;

                Rectangle r2 = new Rectangle(new Point(left, top), imgSize);

                using (Graphics g = this.CreateGraphics())
                {
                    g.DrawImage(imageButton, r2);
                }
            }
            else // Draw Arrow
            {
                SolidBrush brush = new SolidBrush(navigateBarButton.NavigateBar.NavigateBarColorTable.BorderColor);
                int halfHeight = this.Height / 2;
                int halfWidth = this.Width / 2;

                using (Graphics g = this.CreateGraphics())
                {
                    g.FillPolygon(brush, new Point[] { 
                        new Point(halfWidth - ARROW_WIDTH /2 , halfHeight), 
                        new Point(halfWidth + ARROW_WIDTH /2, halfHeight), 
                        new Point(halfWidth, halfHeight + ARROW_HEIGHT) });

                }
            }



        }
        #endregion

    }
}
